Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

125
Vistas
Works with chrome but not safari (?<![\n])[\n]{1}(?![\n])

I have this regex (?<![\n])[\n]{1}(?![\n]) in my Javascript code, and as I understand Safari just doesn't support negative look behinds.

What I need to do is replace all \n that are not surrounded by other \n and replace them with a string.

Can someone please help me figure out how to write that Regex so that it is usable in Safari?

Thank you.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You could capture either the start of the string or a non whitespace character in group 1.

Then match a single newline, and assert not a newline to the right.

In the replacement use the capture group followed by your replacement string $1[replacement]

Note that you don't need the square brackets around the newline, and you can omit {1}

(\S|^)\n(?!\n)

Regex demo

const regex = /(\S|^)\n(?!\n)/g;
const str = `
this is a
this is b
this is c

d


test

test


test
test
`;

const result = str.replace(regex,`$1[replacement]`);

console.log(result);

Your pattern with the lookbehind (if supported):

const regex = /(?<!\n)\n(?!\n)/g;
const str = `
this is a
this is b
this is c

d


test

test


test
test
`;
`[replacement]`
const result = str.replace(regex,`[replacement]`);

console.log(result);

about 4 years ago · Juan Pablo Isaza Denunciar

0

Use

text.replace(/(\n)?\n(?!\n)/g, function(p,q) {
    return q === undefined ? "<a string>":p
})

JS code:

var text = "a\nb\n\nc"
console.log(
  text.replace(/(\n)?\n(?!\n)/g, function(p,q) {
    return q === undefined ? "<a string>":p
}))

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda